package 脏读;

class PublicVar{
	public String username="A";
	public String password="AA";
	synchronized public void setValue(String username,String password)//属于类PublicVar的实例
	{
		try {
			this.username=username;
			Thread.sleep(5000);
			this.password=password;
			setValue();
		}
	catch (InterruptedException e)
		{
			e.printStackTrace();
		}
	}
	synchronized public  void getValue()
	{
		System.out.println("getValue method thread name="+Thread.currentThread().getName()+" username="
				+username+" password="+password);
	}
	 public void setValue() {
	 System.out.println("setValue method thread name="+Thread.currentThread().getName()+" username="
			+username+" password="+password);
	 }
}
class ThreadAA extends Thread{
	private PublicVar object;
	public ThreadAA(PublicVar  object)
	{
		super();
		this.object=object;
	}
	public void run()
	{
		super.run();
		object.setValue("B","BB");
	}
}

public class Test2_11 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			PublicVar publicVarref=new PublicVar();
			ThreadAA threadAA=new ThreadAA(publicVarref);
			threadAA.start();
			Thread.sleep(20);//打印结果受此值影响
			publicVarref.getValue();
		}catch(InterruptedException e)
		{
			e.printStackTrace();
		}

	}

}
